package com.yifeng.repo.controller.statemachine.configure;

import com.yifeng.repo.controller.statemachine.fsm.audit.AuditStatemachine;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.action.AuditDoneAction;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.action.AuditPassAction;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.action.AuditRejectAction;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.condition.AuditDoneCondition;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.condition.AuditPassCondition;
import com.yifeng.repo.controller.statemachine.fsm.audit.worker.condition.AuditRejectCondition;
import com.yifeng.repo.controller.statemachine.web.StatemachineServletRegistrar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by daibing on 2024/9/21.
 */
@Configuration
@EnableConfigurationProperties(StatemachineProperties.class)
@ConditionalOnClass({StatemachineProperties.class})
public class StatemachineConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private StatemachineProperties properties;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = StatemachineProperties.PREFIX, name = "enable-audit-statemachine")
    public AuditStatemachine auditStatemachine(@Autowired(required = false) AuditPassAction auditPassAction,
                                               @Autowired(required = false) AuditRejectAction auditRejectAction,
                                               @Autowired(required = false) AuditDoneAction auditDoneAction,
                                               @Autowired(required = false) AuditPassCondition auditPassCondition,
                                               @Autowired(required = false) AuditRejectCondition auditRejectCondition,
                                               @Autowired(required = false) AuditDoneCondition auditDoneCondition) {
        return new AuditStatemachine(auditPassAction, auditRejectAction, auditDoneAction, auditPassCondition, auditRejectCondition, auditDoneCondition);
    }

    @Bean
    @ConditionalOnProperty(prefix = StatemachineProperties.PREFIX, name = "enable-api")
    public StatemachineServletRegistrar kafkaServletRegistrar() {
        return new StatemachineServletRegistrar(properties.getApiPrefix());
    }
}
